package ca.uwindsor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.AlertDialog;
import android.app.ExpandableListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SimpleExpandableListAdapter;

public class Summary extends ExpandableListActivity {

	List<String> courseList = new ArrayList<String>(); //used to store child data.
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//set window title to sem name
		//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.summary);
		//getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.summary);
		//TextView lblSemName = (TextView)findViewById(R.id.lblSemName);
		//lblSemName.setText(Common.globalTerm);
		//TODO:
		//Summary here
		
	
		
		 //Create Expandable lists
		SimpleExpandableListAdapter expListAdapter =
				new SimpleExpandableListAdapter(
					this,
					createGroupList(),	// groupData describes the first-level entries
					R.layout.child_row,	// Layout for the first-level entries
					new String[] { "course" },	// Key in the groupData maps to display
					new int[] { R.id.lblDay },		// Data under "Day" key goes into this TextView
					createChildList(),	// childData describes second-level entries
					R.layout.child_row,	// Layout for second-level entries
					new String[] { "day", "time" },	// Keys in childData maps to display
					new int[] { R.id.lblDay, R.id.lblTime }	// Data under the keys above go into these TextViews
				);
			setListAdapter( expListAdapter );
		
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	//for menu do not change
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}
	
	//Creates the Course lists and returns the list
	private List<HashMap<String, String>> createGroupList() {
		
		final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
		db.setDATABASE_TABLE("courses");
		db.open();
		
		Cursor ret = db.getEntry("sem_name",Common.globalTerm );
		
		ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
		 
		if(ret.moveToFirst()){
			do{
				//populate the list
				HashMap<String, String> m = new HashMap<String, String>();
			    m.put( "course",ret.getString(2)+"  "+ret.getString(3) );
				result.add( m );
				
				
				//create child list
				//Store all the courses in a list. Later passed to createChildList to create the sub list
				
				courseList.add(ret.getString(2));
				
				
			}while(ret.moveToNext());
		}
		db.close();
		
		  return (List<HashMap<String, String>>)result;
	    }
	
	//Create the child list. Passed in the course list
	private List<ArrayList<HashMap<String, String>>> createChildList() {
		final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
		db.setDATABASE_TABLE("class_schedule");
		db.open();
		
		Iterator<String> listiter = courseList.iterator();
		
		ArrayList<ArrayList<HashMap<String, String>>> result = new ArrayList<ArrayList<HashMap<String, String>>>();
		
		
		while(listiter.hasNext()){
		
		Cursor ret_sch = db.getEntry("course_code",listiter.next() );
		ArrayList<HashMap<String, String>> secList = new ArrayList<HashMap<String, String>>(); //VOILA!!!!
		
			
				
		if(ret_sch.moveToFirst()){
			do{
				
				 	HashMap<String, String> child = new HashMap<String, String>();
					child.put( "day", ret_sch.getString(3));
				    child.put( "time", ret_sch.getString(4)+"  to  "+ret_sch.getString(5) );
					secList.add( child );
				
			}while(ret_sch.moveToNext());
			 result.add( secList ); 
		}else{ 
			//if no schedule for course exists alert the user.
			HashMap<String, String> child = new HashMap<String, String>();
			child.put( "day", "No schedule exists for this course.");
		    child.put( "time", "" );
			secList.add( child );
			result.add( secList ); 
		}
		ret_sch.close();
	//add sub list to main list
		}
		 db.close();
		 
		return (List<ArrayList<HashMap<String, String>>>) result;
	  }
	
	
	//for menu do not change
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		
		switch(item.getItemId())
		{
		case R.id.menuaddcourse:
			startActivity(new Intent ("ca.uwindsor.ADDCOURSES"));
			return true;
		case R.id.menuaddclass:
			startActivity(new Intent ("ca.uwindsor.ADDSCHEDULE"));
			return true;
		case R.id.menuaddassm:
			startActivity(new Intent ("ca.uwindsor.ADDASSM"));
			return true;
		case R.id.viewschedule:
			startActivity(new Intent ("ca.uwindsor.VIEWSCHEDULE"));
			return true;
		case R.id.addmarks:
			startActivity(new Intent ("ca.uwindsor.ADDMARKS"));
			return true;
		case R.id.addpreviouscgpa:
			startActivity(new Intent ("ca.uwindsor.ADDPREVIOUSCGPA"));
			return true;
		case R.id.viewgrades:
			startActivity(new Intent ("ca.uwindsor.VIEWGRADES"));
			return true;
		case R.id.viewtask:
			startActivity(new Intent ("ca.uwindsor.VIEWTASKLIST"));
			return true;
		case R.id.updatepass:
			startActivity(new Intent ("ca.uwindsor.UPDATEPASSWORD"));
			return true;	
		case R.id.viewmarks:
			startActivity(new Intent ("ca.uwindsor.VIEWMARKS"));
			return true;	
		case R.id.about:
			final AlertDialog.Builder alert = new AlertDialog.Builder((Summary.this));
			alert.setTitle("Student Event Manager");
			alert.setIcon(R.drawable.icon);
			alert.setMessage("Created by: \nLav Gupta\nJebin Daniel Varghese\n" +
					"You can find the repository at:\n\t " +
					"http://code.google.com/p/studenteventmanager/");
			alert.setNegativeButton("OK", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
				}

			});
			alert.show();
			return true;	
		}
		return false;
		
	}
}
